package com.ruoyi.project.park.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.project.park.domain.BizParkingCard;
import com.ruoyi.project.park.service.IBizParkingCardService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.park.domain.BizFeeRecord;
import com.ruoyi.project.park.service.IBizFeeRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 停车收费记录Controller
 * 
 * @author ruoyi
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/park/feerecord")
public class BizFeeRecordController extends BaseController
{
    @Autowired
    private IBizFeeRecordService bizFeeRecordService;

    @Autowired
    private IBizParkingCardService bizParkingCardService;

    /**
     * 查询停车收费记录列表
     */
    @PreAuthorize("@ss.hasPermi('park:feerecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizFeeRecord bizFeeRecord)
    {
        //判断当前登录用户是否为管理员
        if(!getLoginUser().getUser().getUserId().equals(1L)){
            //不是管理员
            //去停车卡表查询出当前用户对应的车牌号
//            BizParkingCard bizParkingCard = new BizParkingCard();
//            bizParkingCard.setCardNumber(getUsername());
//            BizParkingCard card = bizParkingCardService.selectBizParkingCardList(bizParkingCard).get(0);
//            bizFeeRecord.setVehicleLicensePlate(card.getVehicleLicensePlate());
            String userName = getLoginUser().getUser().getUserName();
            bizFeeRecord.setCreateBy(userName);
            //create_by
            bizFeeRecord.setCreatBy(userName);

        }
        startPage();
        List<BizFeeRecord> list = bizFeeRecordService.selectBizFeeRecordList(bizFeeRecord);
        return getDataTable(list);
    }

    /**
     * 导出停车收费记录列表
     */
    @PreAuthorize("@ss.hasPermi('park:feerecord:export')")
    @Log(title = "停车收费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizFeeRecord bizFeeRecord)
    {
        List<BizFeeRecord> list = bizFeeRecordService.selectBizFeeRecordList(bizFeeRecord);
        ExcelUtil<BizFeeRecord> util = new ExcelUtil<BizFeeRecord>(BizFeeRecord.class);
        util.exportExcel(response, list, "停车收费记录数据");
    }

    /**
     * 获取停车收费记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('park:feerecord:query')")
    @GetMapping(value = "/{feeRecordId}")
    public AjaxResult getInfo(@PathVariable("feeRecordId") Long feeRecordId)
    {
        return AjaxResult.success(bizFeeRecordService.selectBizFeeRecordByFeeRecordId(feeRecordId));
    }

    /**
     * 新增停车收费记录
     */
    @PreAuthorize("@ss.hasPermi('park:feerecord:add')")
    @Log(title = "停车收费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizFeeRecord bizFeeRecord)
    {
        //生成雪花id
        String nextIdStr = new Snowflake(1, 1).nextIdStr();
        bizFeeRecord.setBillingTime(new Date());
        bizFeeRecord.setFeeRecordId(Long.valueOf(nextIdStr));
        bizFeeRecord.setCreatBy(getUsername());
        bizFeeRecord.setCreateBy(getUsername());
        return toAjax(bizFeeRecordService.insertBizFeeRecord(bizFeeRecord));
    }

    /**
     * 修改停车收费记录
     */
    @PreAuthorize("@ss.hasPermi('park:feerecord:edit')")
    @Log(title = "停车收费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizFeeRecord bizFeeRecord)
    {
        return toAjax(bizFeeRecordService.updateBizFeeRecord(bizFeeRecord));
    }

    /**
     * 删除停车收费记录
     */
    @PreAuthorize("@ss.hasPermi('park:feerecord:remove')")
    @Log(title = "停车收费记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{feeRecordIds}")
    public AjaxResult remove(@PathVariable Long[] feeRecordIds)
    {
        return toAjax(bizFeeRecordService.deleteBizFeeRecordByFeeRecordIds(feeRecordIds));
    }
}
